<template>
  <div class="page-container">
    <div class="page-header">
      <button class="back-button" @click="goToHome">
        <span class="icon-arrow-left"></span>
        返回首页
      </button>
      <h1>客户满意度分析</h1>
      <p>了解客户对服务的评价和反馈</p>
    </div>
    
    <div class="satisfaction-overview">
      <div class="rating-card">
        <div class="rating-average">4.7</div>
        <div class="rating-stars">
          <span class="star filled">★</span>
          <span class="star filled">★</span>
          <span class="star filled">★</span>
          <span class="star filled">★</span>
          <span class="star half-filled">★</span>
        </div>
        <div class="rating-count">基于 248 条评价</div>
      </div>
      
      <div class="rating-distribution">
        <h3>评分分布</h3>
        <div class="rating-bar" v-for="(rating, index) in ratings" :key="index">
          <div class="rating-label">{{ 5 - index }} 星</div>
          <div class="rating-progress">
            <div class="progress-bar" :style="{ width: rating.percentage }"></div>
          </div>
          <div class="rating-percentage">{{ rating.percentage }}</div>
        </div>
      </div>
    </div>
    
    <div class="feedback-section">
      <h2>客户反馈趋势</h2>
      <div class="chart-container">
        <div ref="satisfactionTrendChart" class="chart"></div>
      </div>
    </div>
    
    <div class="feedback-section">
      <h2>最近客户评价</h2>
      <div class="feedback-list">
        <div class="feedback-item" v-for="(feedback, index) in recentFeedback" :key="index">
          <div class="feedback-header">
            <div class="customer-name">{{ feedback.customer }}</div>
            <div class="feedback-stars">
              <span class="star filled" v-for="n in feedback.rating" :key="n">★</span>
              <span class="star" v-for="n in (5 - feedback.rating)" :key="n + 5">★</span>
            </div>
            <div class="feedback-date">{{ feedback.date }}</div>
          </div>
          <div class="feedback-content">{{ feedback.comment }}</div>
          <div class="feedback-ticket">关联工单: #{{ feedback.ticketId }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'CustomerSatisfaction',
  data() {
    return {
      ratings: [
        { percentage: '68%' },
        { percentage: '21%' },
        { percentage: '7%' },
        { percentage: '3%' },
        { percentage: '1%' }
      ],
      recentFeedback: [
        {
          customer: 'ABC公司',
          rating: 5,
          date: '2023-06-15',
          comment: '服务非常及时，技术人员专业，问题很快得到解决。',
          ticketId: 'TK-2023-5874'
        },
        {
          customer: 'XYZ企业',
          rating: 4,
          date: '2023-06-14',
          comment: '整体服务不错，响应时间可以再提高一些。',
          ticketId: 'TK-2023-5869'
        },
        {
          customer: '123贸易',
          rating: 5,
          date: '2023-06-12',
          comment: '技术支持人员非常耐心，一步步指导解决问题，非常满意。',
          ticketId: 'TK-2023-5842'
        }
      ],
      satisfactionChart: null
    };
  },
  methods: {
    goToHome() {
      this.$router.push('/home');
    },
    // 初始化满意度趋势图
    initSatisfactionTrendChart() {
      const chartDom = this.$refs.satisfactionTrendChart
      if (!chartDom) return
      
      // 销毁已有实例
      if (this.satisfactionChart) {
        this.satisfactionChart.dispose()
      }
      
      this.satisfactionChart = echarts.init(chartDom)
      
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: { type: 'shadow' }
        },
        legend: { data: ['满意度评分', '评价数量'] },
        grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
        xAxis: {
          type: 'category',
          data: ['1月', '2月', '3月', '4月', '5月', '6月']
        },
        yAxis: [
          {
            type: 'value',
            name: '评分',
            min: 0,
            max: 5,
            interval: 1,
            axisLabel: { formatter: '{value}' }
          },
          {
            type: 'value',
            name: '数量',
            min: 0,
            position: 'right',
            axisLabel: { formatter: '{value} 条' }
          }
        ],
        series: [
          {
            name: '满意度评分',
            type: 'line',
            data: [4.2, 4.3, 4.5, 4.4, 4.6, 4.7],
            symbol: 'circle',
            symbolSize: 8,
            lineStyle: { width: 3 },
            itemStyle: { color: '#3498db' }
          },
          {
            name: '评价数量',
            type: 'bar',
            yAxisIndex: 1,
            data: [32, 45, 38, 52, 41, 40],
            itemStyle: { color: '#2ecc71' }
          }
        ]
      }
      
      this.satisfactionChart.setOption(option)
    }
  },
  mounted() {
    this.initSatisfactionTrendChart()
    
    // 监听窗口大小变化
    window.addEventListener('resize', () => {
      if (this.satisfactionChart) {
        this.satisfactionChart.resize()
      }
    })
  },
  beforeUnmount() {
    if (this.satisfactionChart) {
      this.satisfactionChart.dispose()
    }
  }
}
</script>

<style scoped>
.page-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
}

.page-header {
  margin-bottom: 30px;
}

.back-button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 15px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s;
  margin-right: 20px;
  font-weight: 500;
}

.back-button:hover {
  background: #2980b9;
  transform: translateY(-2px);
}

.satisfaction-overview {
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: 20px;
  margin-bottom: 30px;
}

.rating-card {
  background: white;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.rating-average {
  font-size: 48px;
  font-weight: bold;
  color: #3498db;
  margin-bottom: 10px;
}

.rating-stars {
  font-size: 24px;
  color: #ddd;
  margin-bottom: 10px;
}

.star.filled {
  color: #f1c40f;
}

.star.half-filled {
  background: linear-gradient(90deg, #f1c40f 50%, #ddd 50%);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.rating-count {
  color: #666;
  font-size: 14px;
}

.rating-distribution {
  background: white;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.rating-bar {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.rating-label {
  width: 50px;
  text-align: right;
  margin-right: 10px;
  font-weight: 500;
}

.rating-progress {
  flex: 1;
  height: 10px;
  background-color: #f1f1f1;
  border-radius: 5px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  background-color: #3498db;
  border-radius: 5px;
}

.rating-percentage {
  width: 50px;
  text-align: left;
  margin-left: 10px;
  font-weight: 500;
}

.feedback-section {
  background: white;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  margin-bottom: 30px;
}

.feedback-section h2 {
  margin-top: 0;
  margin-bottom: 20px;
  color: #333;
}

.chart {
  width: 100%;
  height: 300px;
}

.feedback-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.feedback-item {
  padding: 15px;
  border-radius: 5px;
  background-color: #f8f9fa;
  border-left: 4px solid #3498db;
}

.feedback-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  align-items: center;
}

.customer-name {
  font-weight: 500;
}

.feedback-date {
  font-size: 12px;
  color: #666;
}

.feedback-content {
  margin-bottom: 10px;
  line-height: 1.5;
}

.feedback-ticket {
  font-size: 12px;
  color: #666;
  font-style: italic;
}
</style>